package com.kmt.plaza.sevice.impl;

import com.kmt.plaza.dao.OrdersMapper;
import com.kmt.plaza.dao.ProductCommentsMapper;
import com.kmt.plaza.dao.ProductMapper;
import com.kmt.plaza.dao.ProductParamsMapper;
import com.kmt.plaza.entity.*;
import com.kmt.plaza.sevice.OrderService;
import com.kmt.plaza.sevice.ProductDetailsService;
import com.kmt.plaza.sevice.ProductService;
import com.kmt.plaza.utils.PageHelper;
import com.kmt.plaza.vo.ProductCommentsVO;
import com.kmt.plaza.vo.ResStatus;
import com.kmt.plaza.vo.ResultVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class ProductDetailsServiceImpl implements ProductDetailsService {
    @Autowired
    private ProductCommentsMapper productCommentsMapper;
    @Autowired
    private ProductParamsMapper productParamsMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private OrdersMapper ordeswiperImgrsMapper;
    private Logger logger = LoggerFactory.getLogger(ProductDetailsServiceImpl.class);
    @Override
    public ResultVO findAllComments(String productId,int pageNum, int limit) {
        logger.info("查询某一商品的评论开始......");
        //1、查询总记录数
        Example example = new Example(ProductComments.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("productId",productId);
        int count = productCommentsMapper.selectCountByExample(example);
        //2、计算总页数
        int pageCount = count%limit == 0?count/limit : (count/limit)+1;
        //查询当前页的数据
        int start = (pageNum-1)*limit;
        List<ProductComments> list = productCommentsMapper.findAllComments(productId,start,limit);
        ResultVO resultVO = new ResultVO(ResStatus.OK, "success", new PageHelper<ProductComments>(count, pageCount, list));
        return resultVO;
    }

    /**
     * 商品详情页商品参数的展示
     * @param productId
     * @return
     */
    @Override
    public ResultVO getProductParams(String productId) {
        logger.info("获取商品的参数信息开始......");
        Example example = new Example(ProductParams.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("productId",productId);
        List<ProductParams> productParams = productParamsMapper.selectByExample(example);
        if(productParams.size()>0){
            return new ResultVO(ResStatus.OK,"success",productParams);
        }
        return new ResultVO(ResStatus.NO,"没有查到该条商品的参数",null);
    }

    @Override
    public ResultVO getProductCommentStat(String productId) {
        logger.info("获取商品的评论状态开始......");
        Example example = new Example(ProductComments.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("productId",productId);
        int count = productCommentsMapper.selectCountByExample(example);
        //好评数
        criteria.andEqualTo("commType",3);
        int goodCount = productCommentsMapper.selectCountByExample(example);
        //中评数
        Example example1 = new Example(ProductComments.class);
        Example.Criteria criteria1 = example1.createCriteria();
        criteria1.andEqualTo("productId",productId);
        criteria1.andEqualTo("commType",4);
        int midCount = productCommentsMapper.selectCountByExample(example1);
        //差评数
        Example example2 = new Example(ProductComments.class);
        Example.Criteria criteria2 = example2.createCriteria();
        criteria2.andEqualTo("productId",productId);
        criteria2.andEqualTo("commType",-1);
        int badCount = productCommentsMapper.selectCountByExample(example2);
        //计算好评率
        double percent = Double.parseDouble(goodCount+"")/ Double.parseDouble(count+"");
        String percentValue = (percent+"").substring(0,(percent+"").lastIndexOf(".")+2);
        HashMap<String,Object> map = new HashMap<>();
        map.put("Count",count);
        map.put("goodCount",goodCount);
        map.put("midCount",midCount);
        map.put("badCount",badCount);
        map.put("percent",percentValue);

        ResultVO resultVO = new ResultVO(ResStatus.OK, "success", map);
        return resultVO;
    }

    @Override
    public ResultVO addComment(ProductCommentsVO productCommentsVO) {
        logger.info("为某一商品添加评论开始......");
        int i = productCommentsMapper.addComment(productCommentsVO);
        if(i>0){
            //如果添加成功将订单状态改为已评价
            String orderId = productCommentsVO.getOrderId();
            int a = ordersMapper.updateOrderStatu(orderId);
            return new ResultVO(ResStatus.OK,"success",a);
        }else{
            return new ResultVO(ResStatus.NO,"fail",null);
        }

    }
}
